package com.ksd1.notes.broadcast;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;


import com.ksd1.notes.MainActivity;
import com.ksd1.notes.R;
import com.ksd1.notes.RemainClock;
import com.ksd1.notes.util.TimeTool;

import static android.content.Context.NOTIFICATION_SERVICE;

public class TimeTaskReceiver extends BroadcastReceiver {
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    Context mContext;
    TimeTool timeTool;

    @Override
    public void onReceive(Context context, Intent intent) {

        mContext = context;
        timeTool = TimeTool.getInstance();
        String title = timeTool.getField("title");
        String content = timeTool.getField("content");

        //发送广播
//        Log.d("20182005085", "onReceive: ");
        createNotification(title, content);
        timeTool.setTiming(false);

    }

    /**
     * 创建渠道
     */
    private String createNotificationChannel(String channelID, String channelNAME, int level) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
            manager.createNotificationChannel(channel);
            return channelID;
        } else {
            return null;
        }
    }

    private void createNotification(String title, String content){
        Intent intent = new Intent(mContext, RemainClock.class);
        intent.putExtra("title", title);
        intent.putExtra("content", content);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


        //获取channelId
        String channelId = createNotificationChannel("085", "ksd1", NotificationManager.IMPORTANCE_HIGH);
        NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext, channelId)
                .setContentTitle("待办："+title)
                .setContentText("时间："+content)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.app_icon)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setAutoCancel(true);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext);
        notificationManager.notify(100, notification.build());

    }


}
